package com.easywan.web.controller.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.service.MailService;
import com.easywan.service.UsersService;
import com.easywan.util.MD5Utils;
import com.easywan.util.Utils;
import com.easywan.util.VifUtils;
import com.easywan.vo.Users;
/**
 * 注册控制器
 * @author T
 * 
 */
//@Controller
public class RegisterController {
	private Logger logger = Logger.getLogger(LoginController.class);
	@Autowired
	private UsersService usersService;
	@Autowired
	private MailService emailService;
	@RequestMapping("register")
	public ModelAndView register_html(HttpServletRequest req,HttpServletResponse res,ModelMap modelMap){
		//Utils.sendMsg(res, "<script type=\"text/javascript\">alert(\"暂不开发注册！\");window.location.href=\"\\index.html\";</script>");
		return new ModelAndView("system/register");
	}
	/**
	 * 检查名字是否被注册
	 * @param req
	 * @param res
	 * @param name 用户名
	 */
	@RequestMapping("register/checkname")
	public void checkname(HttpServletRequest req,HttpServletResponse res,@RequestParam("name") String name){
		List<Users> usersList =  usersService.selectByName(name);
		if(Utils.listIsEmpty(usersList)){
			Utils.sendMsg(res, "1");
		}else{
			Utils.sendMsg(res, "0");	
		}
		
	}
	@RequestMapping("register/checkemail")
	public void checkemail(HttpServletRequest req,HttpServletResponse res,@RequestParam("email") String email){
		List<Users> usersList = usersService.selectByEmail(email);
		if(Utils.listIsEmpty(usersList)){
			Utils.sendMsg(res, "1");
		}else{
			Utils.sendMsg(res, "0");	
		}
	}
	/**
	 * 注册信息
	 * @param req
	 * @param res
	 * @param users
	 */
	@RequestMapping("register_chk")
	public void register_chk(HttpServletRequest req,HttpServletResponse res,Users users){
		if(Utils.stringIsEmpty(users.getName())){
			Utils.sendMsg(res, "请输入用户名！");
		}else if(Utils.stringIsEmpty(users.getEmail())){
			Utils.sendMsg(res, "请输入邮箱");
		}else if(usersService.checkUsersEmail(users.getEmail())){
			Utils.sendMsg(res, "邮箱已经被注册");
		}else if(usersService.checkUsersName(users.getName())){
			Utils.sendMsg(res, "用户名以及被注册！");
		}else{
			usersService.addUsers(users);
			//发送验证码
			String code = VifUtils.getVifCode();
			emailService.sendVifcode(users.getEmail(), code);
			HttpSession session = req.getSession();
			session.setAttribute("users", users);
			System.out.println(users.getEmail());
			System.out.println(code);
			session.setAttribute("vifcode", code);
			Utils.sendMsg(res, "1");	
		}
	}
	@RequestMapping("register/vifcode_check")
	public void vifcode_check(@RequestParam("code") String code,HttpSession session,HttpServletResponse res){
		Users users = (Users) session.getAttribute("users");
		if(users == null){
			Utils.sendMsg(res, "0");
			return;
		}
		String scode = (String) session.getAttribute("vifcode");
		if(code != null && code.equals(scode)){
			usersService.vifCode(true,users.getEmail());
			Utils.sendMsg(res, "1");
		}else{
			Utils.sendMsg(res, "0");
		}
	}
	@RequestMapping("register_chk2")
	public void register_chk2(@RequestParam("pwd") String pwd,HttpSession session,HttpServletResponse res){
		Users users = (Users) session.getAttribute("users");
		if(users == null){
			Utils.sendMsg(res, "0");
			return;
		}else{
			Utils.sendMsg(res, "1");
			users.setPwd(MD5Utils.md5(pwd));
			usersService.setPwd(users);
		}
	}
}
